home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / m_to_r / reporter / mastrpt1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  8.9 KB  |  311 lines

  1. unit Mastrpt1;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Reports, StdCtrls, DB, DBTables;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     Query1: TQuery;
  13.     Query1CustNo: TFloatField;
  14.     Query1Company: TStringField;
  15.     Query1Addr1: TStringField;
  16.     Query1Addr2: TStringField;
  17.     Query1City: TStringField;
  18.     Query1State: TStringField;
  19.     Query1Zip: TStringField;
  20.     Query1Country: TStringField;
  21.     Query1Phone: TStringField;
  22.     Query1FAX: TStringField;
  23.     Query1TaxRate: TFloatField;
  24.     Query1Contact: TStringField;
  25.     Query1LastInvoiceDate: TDateTimeField;
  26.     procedure Button1Click(Sender: TObject);
  27.   private
  28.     { Private declarations }
  29.   public
  30.     { Public declarations }
  31.     Procedure ReportInit;
  32.     Procedure ReportDone;
  33.  
  34.     Procedure PrintHeader     ( HeaderBand  : tFixedReportBand;
  35.                                 var Status  : tBandStatus );
  36.  
  37.     Procedure InitItem        ( DetailBand  : tRecurringReportBand );
  38.     Procedure PrintItem       ( DetailBand  : tFixedReportBand;
  39.                                 var Status  : tBandStatus );
  40.  
  41.   end;
  42.  
  43. var
  44.   Form1: TForm1;
  45.  
  46. implementation
  47. Uses pStatus;
  48.  
  49. {$R *.DFM}
  50.  
  51. procedure tForm1.ReportInit;
  52. Begin
  53.      { prevent the user from hitting another key while printing }
  54.      Enabled := False;
  55.  
  56.      With PrintStatusForm Do
  57.      Begin
  58.           ProgressGauge.MaxValue := 100;
  59.           ProgressGauge.Progress := 0;
  60.           CurrentPage := 1;
  61.           PrintStatus := 'Loading Data...';
  62.           PrinterName := Reporter.Printers[Reporter.PrinterIndex];
  63.           Show;
  64.           UpdateStatus;
  65.      End;
  66.  
  67.      { Open needed table(s) }
  68.      Query1.Open;
  69.  
  70.      With PrintStatusForm Do
  71.      Begin
  72.           ProgressGauge.MaxValue := Query1.RecordCount;
  73.           PrintStatus := 'Printing...';
  74.           UpdateStatus;
  75.      End;
  76.  
  77.      { set the margins and define the font styles }
  78.      With Reporter Do
  79.      Begin
  80.           TopMargin.AsInches := 2.0;
  81.           LeftMargin.AsInches := 0.25;
  82.           RightMargin.AsInches := 0.25;
  83.           BottomMargin.AsInches := 1.0;
  84.  
  85.           { Set the Report's default unit to inches }
  86.           PreferredUnit := puInches;
  87.  
  88.           Canvas.Font.Size := 9;
  89.           Canvas.Font.Style := [];
  90.           Canvas.Font.Name  := 'Arial';
  91.           CreateFontStyle ( 'Detail1' );
  92.  
  93.           Canvas.Font.Size := 10;
  94.  
  95.           Canvas.Font.Style := [fsBold];
  96.           CreateFontStyle ( 'Detail2' );
  97.           CreateFontStyle ( 'Heading1' );
  98.           Canvas.Font.Color := clWhite;
  99.           CreateFontStyle ( 'Heading2' );
  100.           Canvas.Font.Color := clBlack;
  101.  
  102.           Canvas.Font.Size := 14;
  103.           CreateFontStyle ( 'Detail3' );
  104.  
  105.           Canvas.Font.Size := 18;
  106.           CreateFontStyle ( 'Heading3' );
  107.  
  108.           Canvas.Font.Size := 42;
  109.           Canvas.Font.Style := [fsBold,fsItalic];
  110.           Canvas.Font.Name  := 'Times New Roman';
  111.           CreateFontStyle ( 'Heading4' );
  112.      End;
  113. End;
  114.  
  115. procedure tForm1.ReportDone;
  116. Begin
  117.      { Close opened table(s) }
  118.      Query1.Close;
  119.  
  120.      { enable the main form and close the print status dialog }
  121.      Enabled := True;
  122.  
  123.      PrintStatusForm.Close;
  124. End;
  125.  
  126. procedure tForm1.PrintHeader (     HeaderBand : tFixedReportBand;
  127.                                var Status     : tBandStatus );
  128. Begin
  129.      With HeaderBand, Reporter Do
  130.      Begin
  131.           { plop a 1/2 inch box onto the bottom of the header.  note that
  132.             I use ConvertWidth to convert inches to dots, even though I've
  133.             set inches as the Reporter's default unit.  This is because
  134.             this output call is made directly to the canvas, not through
  135.             the Reporter's output functions.  The canvas only uses dots/
  136.             pixels, so I need to convert inches to dots }
  137.  
  138.           Canvas.Brush.Color := clDkGray;
  139.           Canvas.Brush.Style := bsSolid;
  140.           Canvas.Rectangle ( Left,  Bottom - ConvertHeight.Inches(0.5).AsDots,
  141.                              Right, Bottom );
  142.           Canvas.Brush.Color := clWhite;
  143.  
  144.  
  145.           { do all the text output now }
  146.  
  147.           { save the current font }
  148.           SaveFont;
  149.  
  150.           { output page number }
  151.           SetFontStyle ( 'Heading1' );
  152.           TopOfBand;
  153.           TextOut ( 'Page ' + IntToStr ( PageNumber ), ttaRightMargin );
  154.  
  155.  
  156.           { output report heading }
  157.           SetFontStyle ( 'Heading3' );
  158.           MoveY ( 0.25 ); { move down 1/4 inch from top of header }
  159.           TextOut ( 'Customer List', ttaBandHCenter );
  160.           NextLine;
  161.  
  162.           SetFontStyle ( 'Heading1' );
  163.           TextOut ( 'By Last Invoice', ttaBandHCenter );
  164.  
  165.           { output company name }
  166.           SetFontStyle ( 'Heading4' );
  167.           Canvas.Brush.Style := bsClear;
  168.           BottomOfBand;
  169.           AdjustY ( -0.5 );
  170.           TextOut ( 'M.A.S.T.', ttaLeftMargin );
  171.  
  172.  
  173.           { output column headings }
  174.           SetFontStyle ( 'Heading2' );
  175.           Canvas.Brush.Style := bsClear;
  176.  
  177.           { position the text just above the bottom of the band }
  178.           BottomOfBand;
  179.           AdjustY ( -0.1 );
  180.  
  181.           TabTo ( 0.655 );
  182.           TextOut ( 'Last Invoice', ttaCenter );
  183.  
  184.           TabTo ( 1.25 );
  185.           TextOut ( 'Address', ttaLeft );
  186.  
  187.           TabTo ( 5.75 );
  188.           TextOut ( 'Fax', ttaLeft );
  189.  
  190.           { move just a little right of the right margin }
  191.           RightOfBand;
  192.           RelativeTabTo ( -0.1 );
  193.           TextOut ( 'Customer No.', ttaRight );
  194.  
  195.           PreviousLine;
  196.           TabTo ( 1.25 );
  197.           TextOut ( 'Customer', ttaLeft );
  198.  
  199.           TabTo ( 5.75 );
  200.           TextOut ( 'Phone', ttaLeft );
  201.  
  202.           { restore previous font }
  203.           Reporter.RestoreFont;
  204.  
  205.      End;
  206. End;
  207.  
  208. procedure tForm1.InitItem ( DetailBand : tRecurringReportBand );
  209. Begin
  210.      { Here we inform the Reporter of the height of the Detail band.
  211.        This allows it to avoid breaking a record across pages.  I'm
  212.        giving each band the height of two 14 point lines }
  213.  
  214.      With DetailBand, Reporter Do
  215.      Begin
  216.           { every output call between SetBandBegin and SetBandEnd is
  217.             used to set the height of the band.  Once this is done, the
  218.             Reporter will not allow a band to be split across pages }
  219.           SetBandBegin;
  220.  
  221.           SaveFont;
  222.  
  223.           SetFontStyle ( 'Detail3' );
  224.           NextLine;
  225.           NextLine;
  226.  
  227.           RestoreFont;
  228.  
  229.           SetBandEnd;
  230.      End;
  231.  
  232.      (DetailBand as tRecurringReportBand).SetBandEnd;
  233. End;
  234.  
  235. procedure tForm1.PrintItem   (     DetailBand : tFixedReportBand;
  236.                                var Status     : tBandStatus );
  237. Begin
  238.      With DetailBand, Reporter Do
  239.      Begin
  240.           SetFontStyle ( 'Detail3' );
  241.           TabTo ( 1.25 );
  242.           TextOut ( Query1Company.AsString, ttaLeft );
  243.           NextLine;
  244.  
  245.           { These two curious statements position us (vertically) to
  246.             where we need to be to print out the phone number.  This
  247.             kind of move is necessary, because the Reporter prints the
  248.             upper left corner of the text at the current X, Y. }
  249.           SetFontStyle ( 'Detail2' );
  250.           PreviousLine;
  251.  
  252.           { position ourselves and output the phone number }
  253.           TabTo ( 5.75 );
  254.           TextOut ( Query1Phone.AsString, ttaLeft );
  255.           NextLine;
  256.  
  257.           { position ourselves and output the last invoice date }
  258.           TabTo ( 0.655 );
  259.           TextOut ( DateToStr(Query1LastInvoiceDate.Value), ttaCenter );
  260.  
  261.           { position ourselves and output the address }
  262.           SetFontStyle ( 'Detail1' );
  263.           TabTo ( 1.25 );
  264.           TextOut ( Query1Addr1.AsString + '  ' +
  265.                     Query1Addr2.AsString + '  ' +
  266.                     Query1City.AsString  + ', ' +
  267.                     Query1State.AsString + ' ' +
  268.                     Query1Zip.AsString + ' ' +
  269.                     Query1Country.AsString,
  270.                     ttaLeft );
  271.  
  272.           { position ourselves and output the fax number }
  273.           SetFontStyle ( 'Detail2' );
  274.           TabTo ( 5.75 );
  275.           TextOut ( Query1Fax.AsString, ttaLeft );
  276.  
  277.           { position ourselves and output the customer number }
  278.           RightOfBand;
  279.           RelativeTabTo ( -0.1 );
  280.           TextOut ( Query1CustNo.AsString, ttaRight );
  281.      End;
  282.  
  283.  
  284.      With PrintStatusForm Do
  285.      Begin
  286.           ProgressGauge.Progress := ProgressGauge.Progress + 1;
  287.           CurrentPage := Reporter.PageNumber;
  288.           UpdateStatus;
  289.      End;
  290.  
  291.      Query1.Next;
  292.      If PrintStatusForm.Canceled Then
  293.         Status := bsAbort
  294.      Else If Query1.EOF Then
  295.         Status := bsDone;
  296. End;
  297.  
  298.  
  299. procedure TForm1.Button1Click(Sender: TObject);
  300. begin
  301.      Reporter.OnReportInit := ReportInit;
  302.      Reporter.OnReportDone := ReportDone;
  303.  
  304.      Reporter.AddHeader ( PrintHeader );
  305.      Reporter.AddDetail ( InitItem, PrintItem );
  306.  
  307.      Reporter.Run;
  308. end;
  309.  
  310. end.
  311.